function getFollowHtml(userinfoId) {
    return '<a href="javascript:;" onclick="follow(' + userinfoId + ',this)" style="color: orange"><i class="layui-icon layui-icon-addition"></i>关注</a>';
}

function getFollowedHtml(userinfoId) {
    return '<a href="javascript:;" onclick="follow(' + userinfoId + ',this)" style="color: green">已关注</a>';
}


function createAuthorList(selector, authorList) {
    let $html = '';
    for (const author of authorList) {
        let $like = '';
        const loginInfo = getLoginInfo() || {};
        if (loginInfo.userinfoId != author.userinfoId) {
            $like = getFollowHtml(author.userinfoId);
            let isFollow = false;
            for (let i = 0; i < author.followList.length; i++) {
                if (author.followList[i].optId == loginInfo.userinfoId) {
                    isFollow = true;
                    break;
                }
            }
            if (isFollow) {
                $like = getFollowedHtml(author.userinfoId);
            }
        }

        $html += '<div class="author-item"><div class="avatar"><a href="page/user/info/' + author.userinfoId + '"><img src="static/upload/' + author.avatar + '" alt=""></a></div><div class="info"><div class="info-group"><p class="author-name">' + author.nickname + '</p><p>写了<span class="article-count">' + author.articleCount + '</span>篇·<span>' + author.followList.length + '</span>人关注</p></div></div><div class="collection">' + $like + '</div></div>';
    }
    $("#" + selector).empty().append($html);
}

function follow(userinfoId, _this) {
    if (getLoginInfo()) {
        http.get('follow/' + userinfoId, null, function () {
            layer.msg("操作成功！", {icon: 6});
            document.dispatchEvent(new Event("tagList"));//广播事件
            if ($(_this).text() === "关注") {
                $(_this).parent().empty().append(getFollowedHtml(userinfoId));
            } else {
                $(_this).parent().empty().append(getFollowHtml(userinfoId));
            }
        })
    } else {
        layer.alert('请先登录！', {icon: 5});
    }
}
